package com.engine.camci.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.camci.utils.CalUtil;
import com.engine.camci.utils.CamciUtil;
import com.engine.camci.utils.ExcelUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-03-10
 * @Description: ${description}
 * @ModifyLog:
 **/

public class WeaCamciWeb {
    /**
     * @Description: 二级台账计算公式：
     * 应发评审费：【二级明细台账】中该专家（以身份证号码为主键）当月会议的【咨询费】合计+【个税】合计；
     * 实发评审费：【二级明细台账】中该专家（以身份证号码为主键）当月会议的【咨询费】合计；
     * 应交个税：【当月总计个税】，实发评审费代入【月总计个税计算公式】计算结果；
     * 已发个税：【二级明细台账】中该专家（以身份证号码为主键）当月会议的【个税】合计；
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/10
     */
    @GET
    @Path("/getSecondLedger")
    @Produces({MediaType.TEXT_PLAIN})
    public String getSecondLedger(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();
            BaseBean baseBean = new BaseBean();
            List<Map<String, Object>> requestMainMapList = new ArrayList<>();
            User user = HrmUserVarify.getUser(request, response);
            int userid = user.getUID();
            String docids = "";
            String month = Util.null2String(request.getParameter("month"));
            baseBean.writeLog(">>>>>getSecondLedger>>>>>" + month);
            //获取当月参与所有会议
            String lastid = "0";
            String ids = "";
            String dataids = "";
            String nodeids = "";
            rs.executeQuery("select nodeid from workflow_flownode where workflowid in (select id from workflow_base where formid='-4')\n" +
                    "and nodeid in (select id from workflow_nodebase where nodename like '%出纳付款' or nodename like '%归档结束')");
            while (rs.next()) {
                String nodeid = Util.null2String(rs.getString(1));
                nodeids += "".equals(nodeids) ? nodeid : "," + nodeid;
            }
            String[] nodeidList = nodeids.split(",");
            rs.executeUpdate("delete uf_ejmxtz where hyny=?", month);
            rs.executeQuery("select id,pzh,hymc from uf_xjhy where left(pzh,7)=? order by right(pzh,4)", month);
            while (rs.next()) {
                List<Map<String, Object>> mapList = new ArrayList<>();
                String id = Util.null2String(rs.getString("id"));
                String hyids = lastid + "," + id;
                ids += "".equals(ids) ? id : "," + id;
                String hymc = Util.null2String(rs.getString("hymc"));
                String pzh = Util.null2String(rs.getString("pzh"));
                baseBean.writeLog(">>>>>getSecondLedger>>>>id=" + id + ",hymc=" + hymc + ",pzh=" + pzh);
                baseBean.writeLog(">>>>hyids=" + hyids + ",ids=" + ids + ",lastid=" + lastid);
                String nodeid = CamciUtil.getNodeidByHymc(id);
                if (CamciUtil.containsStringArray(nodeidList, nodeid)) {
                    rs1.executeQuery("select row_number() over (order by xm)as xh,xm,sfzh,ffje " +
                            "from formtable_main_4_dt1 where hymc=?", id);
                    while (rs1.next()) {
                        Map<String, Object> dataMap = new HashMap<>();
                        String xh = Util.null2String(rs1.getString("xh"));
                        String xm = Util.null2String(rs1.getString("xm"));
                        String sfzh = Util.null2String(rs1.getString("sfzh"));
                        String ffje = Util.null2String(rs1.getString("ffje"));
                        Map<String, Object> resultMap = CamciUtil.getPsfBySfzh(ids, sfzh);
                        String yfpsf = Util.null2String(resultMap.get("yfpsf"));
                        String sfpsf = Util.null2String(resultMap.get("sfpsf"));
                        String yjgs = Util.null2String(resultMap.get("yjgs"));
                        String dkgs = CamciUtil.getDkgsBySfzh(ids, sfzh);
                        dataMap.put("xh", xh);
                        dataMap.put("xm", xm);
                        dataMap.put("sfzh", sfzh);
                        dataMap.put("sfje", ffje);
                        dataMap.put("dkgs", dkgs);
                        dataMap.put("yfje", CalUtil.fAdd(ffje, dkgs));
                        mapList.add(dataMap);
                        requestMainMapList.add(dataMap);
                        dataMap.put("hymc", id);
                        dataMap.put("pzh", pzh);
                        dataMap.put("yjgs", yjgs);
                        dataMap.put("yfgs", yjgs);
                        dataMap.put("yfpsf", yfpsf);
                        dataMap.put("sfpsf", sfpsf);
                        dataMap.put("hyny", month);
                        baseBean.writeLog(">>>>>dataMap" + dataMap.toString());
                        int dataid = CamciUtil.insertModelData(3, userid, dataMap);
                        dataids += "".equals(dataids) ? dataid : "," + dataid;
                    }
                    lastid = id;
                    int docid = ExcelUtil.generateTaxInfoData(hymc, user.getLastname(), "姜宏岩", mapList);
                    docids += "".equals(docids) ? docid : "," + docid;
                }
            }
            String requestid = CamciUtil.createWorkflow(user, docids, requestMainMapList);
            rs.executeUpdate("update uf_ejmxtz set requestid='" + requestid + "' where id in (" + dataids + ")");
            apidatas.put("requestid", requestid);
            apidatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("error_message", "请求接口异常");
        }
        return JSONObject.toJSONString(apidatas);
    }

}
